 /*  
 *  Copyright (C) 2005 Christopher Taylor <christopher.paul.taylor@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
 // summarizes a string
 function ffots_summarize(text)
{
	if(text == '')
	{
		alert("There has been a text selection error. Please highlight text from the webpage you wanted to summarize, then click the 'Summarize' button.");
		return;
	}
	
    // try to access libots by way of XPCOM
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		const componentCID = '{9AA46F4F-4DC7-4c06-97AF-5035170633FE}';
		const cid = "@ct.org/app-text/OTSComponent;1";
		obj = Components.classes[cid].createInstance();
		obj = obj.QueryInterface(Components.interfaces.IOTSComponent);
      
		//gets the machine's components directory
      		componentDir = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get('ComsD', Components.interfaces.nsIFile);
	}
	catch(err) {
		alert("Runtime Error: Firefox XPCOM unable to find OTSComponent.\t" + err);
		return;
	}

    // try to conduct the text summary operation
	try {
		// returns the user's profile info
		//componentDir = Components.classes['@mozilla.org/extensions/manager;1'].getService(Components.interfaces.nsIExtensionManager).getInstallLocation(componentCID).location.path;

		highlighted = document.getElementById('SummarizeHighlight').checked;
		percent = parseInt(document.getElementById('SummarizePercentage').value);
		dict = document.getElementById('SummarizeLanguageMenu').value;
		docSummaryTitle = document.title + ' Summary';
		
		summary = obj.Summary(componentDir.path+'/'+dict, text, highlighted, percent);
		
        // either highlight the summary in the input string or just summarize the input string
		if(!highlighted)
		{
			preface = "<html><body bgcolor='#FFFFCC'><h1>Summary";
			if(text == '')
				preface += " Error!</h1><font color='#16569E'>";
			else
				preface += "</h1><font color='#16569E'>";
			endex = "</font><p>If there was an unexpected problem, please contact the developer by visiting <a href='http://sourceforge.net/projects/ffots'>this website</a>.</p></body></html>";
			summary = preface + summary + endex;
		}
		
		// tab summary
        if(document.getElementById('SummarizeTab').checked)
		{
			windowMediatorContractID = '@mozilla.org/appshell/window-mediator;1';
			windowMediatorIID = Components.interfaces.nsIWindowMediator;
			windowMediator = Components.classes[windowMediatorContractID].getService(windowMediatorIID);
			var browserWindow = windowMediator.getMostRecentWindow('navigator:browser');
			var browser = browserWindow.getBrowser();
			var tab = browser.addTab('');

			tabBrowser = browser.getBrowserForTab(tab);
			tabBrowserWin = tabBrowser.contentWindow;

			tabBrowserWin.document.write(summary);
			tabBrowserWin.document.close();
			tabBrowserWin.document.title = docSummaryTitle;
        }
        // open a new window and display the summary
        else
        {
			win = window.open('');
         		win.document.write(summary);
			win.document.title = docSummaryTitle;
 	 		win.document.close();
        }
	// catch any error throw up an alert dialog with a generic processing error message
	}
	catch(err2)
	{
		alert("Runtime Error: Firefox XPCOM experienced difficulty executing OTSComponent.\t" + err2);
		alert(err2);
		return;
	}
}